﻿/// <reference path="../../js/public.js" />

$$(function () {
    //页面下拉分页加载初始化
    initPageLoding(ing_loading);
    //顶部tab切换
    $$(".buttons-row a").each(function () {
        var _this = $$(this);
        _this.click(function () {
            set_tab(_this.index());
        });
    });
});
var ing_loading = true, done_loading = true;
//加载界面数据
function initPageLoding(loading, opt) {
    if (!loading) {
        return;
    }
    opt = $.extend({
        pageSize: 20,
        containerSelector: "#manage-content-ing",
        listSelector: "#xyh-manage-ing-list-ul",
        loadData: ajaxLoadPageData,
        render: appendItems,
        renderEmpty: function (jqList, jqContainer) { $.toast("暂无数据！"); }
    }, opt);
    //页面下拉分页加载初始化
    $$.initPagingLoader(opt);
}
//当前tab索引值
var currIndex = 0;//0:进行中,1:已完成
//顶部Tab切换
function set_tab(index) {
    var tabs = $$(".buttons-row a");
    tabs.each(function () {
        var _this = $$(this);
        _this.removeClass("active");
        if (_this.index() == index) {
            _this.addClass("active");
            currIndex = index;
            content_show(index == 0);
        }
    });
}
//内容容器显隐
function content_show(isShow) {
    if (isShow) {
        $$("#manage-content-ing").show();
        $$("#manage-content-done").hide();
    } else {
        if (done_loading) {
            var opt = {
                containerSelector: "#manage-content-done",
                listSelector: "#xyh-manage-done-list-ul",
            };
            initPageLoding(done_loading, opt);
            done_loading = false;
        }
        $$("#manage-content-done").show();
        $$("#manage-content-ing").hide();
    }
}
//获取页数据
function ajaxLoadPageData(args) {
    var pageIndex = args.pageIndex;
    var pageSize = args.pageSize;
    var ar = args.ar;
    var eventType = args.eventType;

    console.dir(args);
    //ar.fail() 失败时调用
    //ar.done(data) 成功时调用，data:null 表示无数据

    console.log("加载页：" + pageIndex);

    //参数
    var postData = {
        pageIndex: pageIndex,
        pageSize: pageSize,
        type: currIndex//0:进行中,1:已完成
    };

    //返回值数据结构
    var data = {
        totalRecordCount: 25,
        records: [{
            link: "#",//详情链接
            title: "信用行信用购0期",//标题
            incom_pre: "90%",//预期收益率
            period: "0天",//周期
            money: (2000 * Math.random()).toFixed(2)//投资金额
        }]
    };
    //模拟 ajax 过程
    setTimeout(function () {
        data.records = [];
        for (var i = 0; i < pageSize; i++) {
            data.records[i] = {
                link: "#",//详情链接
                title: "信用行信用购" + (i + 1) + "期",//标题
                incom_pre: "90%",//预期收益率
                period: ((i + 1) * 7) + "天",//周期
                money: (i % 2 == 0 ? "" : "-") + (2000 * Math.random()).toFixed(2)//投资金额
            };
        }
        ar.done(data);
    }, 1000);
}

//动态新增条目
function appendItems(data, jqList, jqContainer) {
    var record, html, item;
    for (var i = 0; i < data.records.length; i++) {
        var record = data.records[i];
        html = item_data_bind(record);
        item = $$(html);
        item.appendTo(jqList);
    }
}
//单条数据拼接
function item_data_bind(record) {
    var _item = "";
    _item = "<li class=\"card\">" +
            "<a href=\"" + record.link + "\" class=\"item-link item-content\">" +
                "<div class=\"manage-content\">" +
                "<div>" + record.title + "</div>" +
                    "<table>" +
                        "<thead>" +
                            "<tr><th>预期收益率</th><th>周期</th><th>投资金额</th></tr>" +
                        "</thead>" +
                        "<tbody>" +
                            "<tr><td>" + record.incom_pre + "</td><td>" + record.period + "</td><td>" + record.money + "</td></tr>" +
                        "</tbody>" +
                    "</table>" +
                    "<span class=\"icon icon-right\"></span>" +
                "</div>" +
             "</a>" +
             "</li>";
    return _item;
}